<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\BondFanModel;
use app\index\model\BondJiaoModel;
use app\index\model\BondKaiModel;
use app\index\model\BondShouModel;
use app\index\model\BondTuiModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DaiKouQiListModel;
use app\index\model\DaiKouQiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBianModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginGodanListModel;
use app\index\model\EnginGodanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginZhengModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\MenuModel;
use app\index\model\OptionFeiModel;
use app\index\model\OptionModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxModel;
use app\index\model\ShouXiangKouModel;
use app\index\model\ShouXiangModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\UserModel;
use app\index\model\ZhiJieModel;
use app\index\model\ZhiLianjieModel;
use app\index\model\ZhiXiangModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BianhaoService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Engin extends Common
{

    public function list_engin_dao(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number','like', '%'.$param['keyword'].'%'];
        }

        $whereMiss = [];
        if($this->userinfo['token_roletype'] == 1){
            //全部权限
        }
        if($this->userinfo['token_roletype'] == 2){
            $enginArr = (new EnginService())->get_user_engin();
            $whereMiss[] = ['e.id','in',$enginArr];
        }
        if($this->userinfo['token_roletype'] == 3){
            $lianerarr = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where('id','in',$this->userinfo['lianer'])->column('name');
            $enginArr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'business'=>2])
                ->where('lian_unit','in',$lianerarr)->column('id');
            $whereMiss[] = ['e.id','in',$enginArr];
        }
        if($this->userinfo['token_roletype'] == 4){
            $where[] = ['e.id','<',0];
        }
        if($this->userinfo['token_roletype'] == 5){
            $userChanid = (new UserService())->get_user_chanid();
            $whereMiss[] = ['e.chanid','=',$userChanid];
        }

        $data = EnginModel::alias('e')
            ->field('e.id,e.name as title')
            ->where($where)
            ->where($whereMiss)
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['ziyao']) && !empty($param['ziyao'])){
            $where[] = ['e.ziyao','like', '%'.$param['ziyao'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['kuang']) && !empty($param['kuang'])){
            $where[] = ['e.kuang','like', '%'.$param['kuang'].'%'];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['e.zhao_unit','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['shikaidt']) && !empty($param['shikaidt'])){
            $where[] = ['e.shikaidt','between', explode(' - ', $param['shikaidt'])];
        }
        if(isset($param['shijundt']) && !empty($param['shijundt'])){
            $where[] = ['e.shijundt','between', explode(' - ', $param['shijundt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name,bill.sericnum,bill.modename')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->leftJoin('flow_bill bill','bill.id=e.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_win(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','in',[0,1]];
        $where[] = ['e.state','in',[3,7,9]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['ziyao']) && !empty($param['ziyao'])){
            $where[] = ['e.ziyao','like', '%'.$param['ziyao'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['kuang']) && !empty($param['kuang'])){
            $where[] = ['e.kuang','like', '%'.$param['kuang'].'%'];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['e.zhao_unit','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['shikaidt']) && !empty($param['shikaidt'])){
            $where[] = ['e.shikaidt','between', explode(' - ', $param['shikaidt'])];
        }
        if(isset($param['shijundt']) && !empty($param['shijundt'])){
            $where[] = ['e.shijundt','between', explode(' - ', $param['shijundt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_win');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_bian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['u.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bianername']) && !empty($param['bianername'])){
            $where[] = ['u.realname','like', '%'.$param['bianername'].'%'];
        }


        $dtSeaArr = [
            'dt_ben' => [date("Y-m-01 00:00:00"), date("Y-m-t 23:59:59")],
            'dt_shang' => [date("Y-m-01 00:00:00", strtotime('last month')), date("Y-m-t 23:59:59", strtotime('last month'))],
            'dt_san' => [date("Y-m-01 00:00:00", strtotime('-2 month')), date("Y-m-t 23:59:59")],
            'dt_year' => [date("Y-01-01 00:00:00"), date("Y-12-31 23:59:59")]
        ];


        $data['data'] = EnginBianModel::alias('bian')
            ->field("bian.bianer,u.realname as bianername")
            ->join('engin e','e.id=bian.enginid')
            ->join('user u','u.id=bian.bianer')
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where($where)
            ->group('bian.bianer')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($dtSeaArr){
                $item['num_all'] = EnginBianModel::where(['bianer'=>$item['bianer']])->group('enginid')->count();
                $item['num_tou'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.state'=>1])->group('bian.enginid')->count();

                $item['num_ben_win'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_ben'])
                    ->group('bian.enginid')->count();
                $item['num_ben_no'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'未中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_ben'])
                    ->group('bian.enginid')->count();

                $item['num_shang_win'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_shang'])
                    ->group('bian.enginid')->count();
                $item['num_shang_no'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'未中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_shang'])
                    ->group('bian.enginid')->count();

                $item['num_san_win'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_san'])
                    ->group('bian.enginid')->count();
                $item['num_san_no'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'未中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_san'])
                    ->group('bian.enginid')->count();

                $item['num_year_win'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_year'])
                    ->group('bian.enginid')->count();
                $item['num_year_no'] = EnginBianModel::alias('bian')->join('engin e','e.id=bian.enginid')
                    ->where(['bian.bianer'=>$item['bianer'],'e.kaiguo'=>'未中标'])
                    ->where('e.kaidt', 'between',$dtSeaArr['dt_year'])
                    ->group('bian.enginid')->count();

                return $item;
            })->toArray();





        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_tongtou_zuo(Request $request){
        $param = $request->post();

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        $year = $request->param('year', date("Y"), 'int');
        $chanid = $request->param('chanid', (new UserService())->get_user_chanid(), 'int');

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = $chanid;
        $data['is_year'] = $year;

        $data['niantounum'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->count();
        $data['nianzaofee'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->sum('e.yuzaofee');

        $data['nianwinnum'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->where('e.state','in',[3,7,9])
            ->count();
        $data['nianwinfee'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->where('e.state','in',[3,7,9])
            ->sum('e.yuzaofee');

        $type_option_id = OptionModel::get_pk_by_num($this->userinfo['comid'], 'engin_engintype');
        $data['xmlxgczj'] = OptionModel::field('id,name,keyid')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$type_option_id,'status'=>1])
            ->order('sort ASC')->select()->toArray();
        foreach ($data['xmlxgczj'] as &$vvv){
            $vvv['num'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.engintype'=>$vvv['keyid']])
                ->count();
            $vvv['value'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.engintype'=>$vvv['keyid']])
                ->sum('e.yuzaofee');
        }
        $oneSort = array_column($data['xmlxgczj'],'value');
        array_multisort($oneSort,SORT_DESC,$data['xmlxgczj']);


        $data['xmsxgczj'] = [
            ['name'=>'自营', 'business'=>1],
            ['name'=>'联营', 'business'=>2],
        ];
        foreach ($data['xmsxgczj'] as &$fff){
            $fff['num'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.business'=>$fff['business']])
                ->count();
            $fff['value'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.business'=>$fff['business']])
                ->sum('e.yuzaofee');
        }
        $oneSort = array_column($data['xmsxgczj'],'value');
        array_multisort($oneSort,SORT_DESC,$data['xmsxgczj']);



        $data['winlist'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->where('e.state','in',[3,7,9])
            ->select()->toArray();
        foreach ($data['winlist'] as &$www){
            $www['state_name'] = (new EnginService())->get_engin_state_name($www['state'], $www['status']);
            $www['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $www['engintype']);
            $www['business_name'] = $www['business'] == 1 ? '自营' : '联营';
            $www['chanid_name'] = DeptModel::get_name_by_pk($www['chanid']);
        }


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_tongtou_you(Request $request){
        $param = $request->post();

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        $chanid = $request->param('chanid', (new UserService())->get_user_chanid(), 'int');

        $startTime = date("Y-m-01 00:00:00", time());
        $endTime = date("Y-m-t 23:59:59", time());


        $data['tou'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.enddt_tou','between',[$startTime, $endTime])
            ->where(['e.chanid'=>$chanid])
            ->select()->toArray();


        $data['kai'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.kaidt','between',[$startTime, $endTime])
            ->where(['e.chanid'=>$chanid])
            ->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_tonggong_info(Request $request){
        $param = $request->post();

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        $year = $request->param('year', date("Y"), 'int');
        $chanid = $request->param('chanid', (new UserService())->get_user_chanid(), 'int');

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = $chanid;
        $data['is_year'] = $year;


        $data['niankainum'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->count();
        $data['nianhefee'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->sum('e.shimoney');

        $data['nianjunnum'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shijundt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->count();

        $data['niandainum'] = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid])
            ->where('e.state','in',[3,7,9])
            ->count();


        //项目状态-合同造价
        $xmzt_win_num = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>3])
            ->count();
        $xmzt_win_value = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>3])
            ->sum('e.shimoney');

        $xmzt_zai_num = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>7])
            ->count();
        $xmzt_zai_value = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>7])
            ->sum('e.shimoney');

        $xmzt_jun_num = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shijundt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>9])
            ->count();
        $xmzt_jun_value = EnginModel::alias('e')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.shijundt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid, 'e.state'=>9])
            ->sum('e.shimoney');

        $data['xmzthtzj'] = [
            ['name'=>'中标', 'num'=>$xmzt_win_num, 'value'=>$xmzt_win_value],
            ['name'=>'在建', 'num'=>$xmzt_zai_num, 'value'=>$xmzt_zai_value],
            ['name'=>'竣工', 'num'=>$xmzt_jun_num, 'value'=>$xmzt_jun_value],
        ];
        $oneSort = array_column($data['xmzthtzj'],'value');
        array_multisort($oneSort,SORT_DESC,$data['xmzthtzj']);



        //开工项目类型-合同造价
        $type_option_id = OptionModel::get_pk_by_num($this->userinfo['comid'], 'engin_engintype');
        $data['xmlxhtzj'] = OptionModel::field('id,name,keyid')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$type_option_id,'status'=>1])
            ->order('sort ASC')->select()->toArray();
        foreach ($data['xmlxhtzj'] as &$vvv){
            $vvv['num'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.engintype'=>$vvv['keyid']])
                ->count();
            $vvv['value'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.shikaidt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.engintype'=>$vvv['keyid']])
                ->sum('e.shimoney');
        }
        $oneSort = array_column($data['xmlxhtzj'],'value');
        array_multisort($oneSort,SORT_DESC,$data['xmlxhtzj']);




        //项目属性-中标工程价
        $data['xmsxhtzj'] = [
            ['name'=>'自营', 'business'=>1],
            ['name'=>'联营', 'business'=>2],
        ];
        foreach ($data['xmsxhtzj'] as &$fff){
            $fff['num'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.business'=>$fff['business']])
                ->count();
            $fff['value'] = EnginModel::alias('e')
                ->where($where)
                ->where((new MenuService())->get_miss_where('engin', 'e'))
                ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
                ->where(['e.chanid'=>$chanid, 'e.business'=>$fff['business']])
                ->sum('e.shimoney');
        }
        $oneSort = array_column($data['xmsxhtzj'],'value');
        array_multisort($oneSort,SORT_DESC,$data['xmsxhtzj']);


        //年度中标-自营项目排行
        $data['winzilist'] = EnginModel::alias('e')
            ->field('id,name,shimoney')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid,'business'=>1])
            ->order('e.shimoney DESC')
            ->limit(10)
            ->select()->toArray();


        //年度中标-联营项目排行
        $data['winlianlist'] = EnginModel::alias('e')
            ->field('id,name,shimoney')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->where('e.windt','between',[$year."-01-01 00:00:00", $year."-12-31 23:59:59"])
            ->where(['e.chanid'=>$chanid,'business'=>2])
            ->order('e.shimoney DESC')
            ->limit(10)
            ->select()->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','in',[0,1]];
        $where[] = ['e.state','in',[3,7,9]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['ziyao']) && !empty($param['ziyao'])){
            $where[] = ['e.ziyao','like', '%'.$param['ziyao'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['kuang']) && !empty($param['kuang'])){
            $where[] = ['e.kuang','like', '%'.$param['kuang'].'%'];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['e.zhao_unit','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['shikaidt']) && !empty($param['shikaidt'])){
            $where[] = ['e.shikaidt','between', explode(' - ', $param['shikaidt'])];
        }
        if(isset($param['shijundt']) && !empty($param['shijundt'])){
            $where[] = ['e.shijundt','between', explode(' - ', $param['shijundt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name,bill.sericnum,bill.modename')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->leftJoin('flow_bill bill','bill.id=e.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_gong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['name'] = trim($param['name']);
            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new EnginService())->check_engin_name($param['name'], $param['id'])){
                return outputJson(4000, '项目名称已存在');
            }
            if(empty($param['engintype'])){
                return outputJson(4000, '请选择项目类型');
            }
            if(empty($param['zhaotype'])){
                return outputJson(4000, '请选择招标方式');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择项目所属单位');
            }
            if(empty($param['business'])){
                return outputJson(4000, '请选择项目属性');
            }
            if($param['business'] == 2){
                if(empty($param['lian_unit'])){
                    return outputJson(4000, '请选择联营方/委托方');
                }
            }else{
                $param['lian_unit'] = '';
            }
            if(empty($param['touunit'])){
                return outputJson(4000, '请选择参投单位');
            }
            if($param['kaiguo'] != '中标'){
                $param['winunit'] = '';
                $param['winfee'] = '';
                $param['windt'] = '';
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);
                (new OptService())->relation_file($files,'engin',$param['id']);
                (new EnginService())->set_engin_state($param['id']);
                (new EnginService())->set_engin_bianer($param['id']);
                (new EnginRelevan())->vate_engin_file('engin', $param['id']);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['data']['touunit_info'] = (new UserService())->get_users_part_arr($data['data']['touunit'], 'dept');
            $data['data']['winunit_info'] = (new UserService())->get_users_part_arr($data['data']['winunit'], 'dept');
            $data['data']['bianer_info'] = (new UserService())->get_users_part_arr($data['data']['bianer'], 'user');
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();

            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function tou_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['touunit'])){
                return outputJson(4000, '请选择参投单位');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);

                (new EnginService())->set_engin_bianer($param['id']);
                (new OptService())->relation_file($files,'engin',$param['id']);
                (new EnginRelevan())->vate_engin_file('engin', $param['id']);


                (new OptService())->add_flow_log('投标', 'engin', $param['id']);
                (new OptService())->add_log_list('投标', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['touunit_info'] = (new UserService())->get_users_part_arr($data['data']['touunit'], 'dept');
            $data['data']['bianer_info'] = (new UserService())->get_users_part_arr($data['data']['bianer'], 'user');

            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['kaiguo'] == '中标'){
                if(empty($param['winunit'])){
                    return outputJson(4000, '请选择中标单位');
                }
                if($param['winfee'] == ''){
                    return outputJson(4000, '请输入中标金额');
                }
            }else{
                $param['winunit'] = '';
                $param['winfee'] = '';
                $param['windt'] = '';
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);
                (new OptService())->relation_file($files,'engin',$param['id']);

                (new EnginService())->set_engin_state($param['id']);
                (new EnginRelevan())->vate_engin_file('engin', $param['id']);


                (new OptService())->add_flow_log('开标', 'engin', $param['id']);
                (new OptService())->add_log_list('开标', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['winunit_info'] = (new UserService())->get_users_part_arr($data['data']['winunit'], 'dept');

            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin(Request $request){
        $param = $request->post();
        $data = EnginModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin', $data['id'], '项目');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function kaijun_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['shikaidt']){
                if($param['shijundt'] < $param['shikaidt']){
                    return outputJson(4000, '竣工验收日期不能小于实际开工日期');
                }
            }else{
                if($param['shijundt'] > date('Y-m-d')){
                    return outputJson(4000, '竣工验收日期应小于当前日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);
                (new OptService())->relation_file($files,'engin',$param['id']);
                (new EnginService())->set_engin_state($param['id']);
                (new EnginRelevan())->vate_engin_file('engin', $param['id']);

                (new OptService())->add_flow_log('开竣工', 'engin', $param['id']);
                (new OptService())->add_log_list('开竣工', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function manage_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);
                (new OptService())->add_flow_log('项目管理组', 'engin', $param['id']);
                (new OptService())->add_log_list('项目管理组', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
            $data['data']['man_xiang_info'] = (new UserService())->get_users_part_arr($data['data']['man_xiang'], 'user');
            $data['data']['man_zhuan_info'] = (new UserService())->get_users_part_arr($data['data']['man_zhuan'], 'user');
            $data['data']['man_zi_info'] = (new UserService())->get_users_part_arr($data['data']['man_zi'], 'user');
            $data['data']['man_cheng_info'] = (new UserService())->get_users_part_arr($data['data']['man_cheng'], 'user');
            $data['data']['man_zhian_info'] = (new UserService())->get_users_part_arr($data['data']['man_zhian'], 'user');

            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function show_engin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $data['data']['engintype']);
        $data['data']['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $data['data']['zhaotype']);
        $data['data']['state_name'] = (new EnginService())->get_engin_state_name($data['data']['state'], $data['data']['status']);
        $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['data']['man_xiang_name'] = UserModel::where(['id'=>$data['data']['man_xiang']])->value('realname');

        $touunit = DeptModel::where(['id'=>$data['data']['touunit']])->column('name');
        $data['data']['touunit_name'] = implode(',', $touunit);

        $bianer = UserModel::where(['id'=>$data['data']['bianer']])->column('realname');
        $data['data']['bianer_name'] = implode(',', $bianer);

        $data['data']['winunit_name'] = DeptModel::where(['id'=>$data['data']['winunit']])->value('name');

        $data['data']['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();



        //工程查询页面详情页要用的数据
        $shiArr['zong_wanbi'] = 0;
        $shiArr['zong_wanchan'] = 0;
        $shiArr['zong_shijitian'] = 0;
        $shiArr['zong_jinbi'] = 0;
        $speedZong = SpeedZongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['data']['id']])->find();
        if($speedZong){
            $speedZong['findt'] = '';
            $zongList = SpeedZongListModel::where(['zongid'=>$speedZong['id']])->select()->toArray();
            foreach ($zongList as $v){
                $findt = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->value('list.findt');
                if(!empty($findt)){
                    if(empty($speedZong['findt'])){
                        $speedZong['findt'] = $findt;
                    }else{
                        if($findt <= $speedZong['findt']){
                            $speedZong['findt'] = $findt;
                        }
                    }
                }
                $shinum = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->sum('list.shinum');
                $shiArr['zong_wanchan'] += $shinum*$v['price'];
            }

            if($speedZong['chanfee'] != 0){
                $shiArr['zong_wanbi'] = round($shiArr['zong_wanchan'] / $speedZong['chanfee'] *100, 2);
            }

            if(!empty($speedZong['findt']) && $speedZong['findt'] != '0000-00-00'){
                $jihua = get_time_slot_days($speedZong['startdt'], $speedZong['enddt']);
                $shiArr['zong_shijitian'] = get_time_slot_days($speedZong['findt'], date("Y-m-d"));
                if($jihua != 0){
                    $shiArr['zong_jinbi'] = round($shiArr['zong_shijitian']/$jihua*100, 2);
                }
            }
            if(!$shiArr['zong_jinbi']){
                $shiArr['zong_jinbi'] = 0;
            }
        }
        $shiArr['zong_jinyu'] = '正常';
        if($shiArr['zong_wanbi'] < $shiArr['zong_jinbi']){
            $shiArr['zong_jinyu'] = '滞后';
        }
        $data['data']['shidata'] = $shiArr;

        $data['data']['zheng_all'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['data']['id']])->count();
        $data['data']['zheng_um'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['data']['id'],'gaista'=>1])->count();
        $data['data']['zheng_no'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['data']['id'],'gaista'=>0])->count();
        $data['data']['zheng_yu'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['data']['id'],'gaista'=>0])->where('dt', '<', date("Y-m-d"))->count();


        $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_cha(Request $request){
        $seatype = $request->post('seatype', '', 'string');
        $seavalue = $request->post('seavalue', '', 'string');
        if(!$seatype || !$seavalue){
            return outputJson(2000,lang('code_2000'), []);
        }

        $seavalueArr = explode(' ', $seavalue);
        $seaStr = "%";
        foreach ($seavalueArr as $vvv){
            if($vvv){
                $seaStr .= $vvv.'%';
            }
        }
        if($seatype == 'name'){
            $whereSea = "e.".$seatype." like '".$seaStr."'";
        }else if($seatype == 'lian_unit'){
            $whereSea = "e.".$seatype." like '".$seaStr."'";
        }else if($seatype == 'man_xiang_name'){
            $whereSea = "xiang.realname like '".$seaStr."'";
        }





        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','in',[0,1]];
        $where[] = ['e.state','in',[3,7,9]];


        $list = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->where($where)
            ->where(Db::raw($whereSea))
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->limit(10)
            ->select()->toArray();

        foreach ($list as &$item){
            if($seatype == 'name'){
                foreach ($seavalueArr as $vvv){
                    if($vvv){
                        $item['name'] = str_replace($vvv, '<b class="colorhong">'.$vvv.'</b>', $item['name']);
                    }
                }
            }

            $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
            $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
            $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'), $list);
    }
    public function dan_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $data['business_name'] = $data['business'] == 1 ? '自营' : '联营';
        $data['state_name'] = (new EnginService())->get_engin_state_name($data['state'], $data['status']);
        $data['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $data['engintype']);
        $data['man_xiang_name'] = UserModel::where(['id'=>$data['man_xiang']])->value('realname');




        $authlanbut = [
            ['id'=>'shou', 'name'=>'收入管理', 'down'=>[
                   [ 'title'=>'收入合同', 'id'=>'contract_shi'],
                   [ 'title'=>'项目签证', 'id'=>'engin_visa'],
                   [ 'title'=>'产值月报', 'id'=>'engin_chan'],
                   [ 'title'=>'收入合同结算', 'id'=>'contract_shi_jie'],
                   [ 'title'=>'项目开票', 'id'=>'piao_kai'],
                   [ 'title'=>'项目收款', 'id'=>'shou_xiang'],
                   [ 'title'=>'项目收入台账-自营', 'id'=>'tai_shou_zi','business'=>1],
                   [ 'title'=>'项目收入台账-联营', 'id'=>'tai_shou_lian','business'=>2],
            ]],
            ['id'=>'zhi', 'name'=>'支出管理', 'down'=>[
                [ 'title'=>'支出合同', 'id'=>'contract_cai'],
                [ 'title'=>'支出合同结算', 'id'=>'contract_cai_jie'],
                [ 'title'=>'采购单', 'id'=>'contract_buy'],
                [ 'title'=>'项目收票', 'id'=>'piao_shou'],
                [ 'title'=>'项目付款', 'id'=>'zhi_xiang'],
                [ 'title'=>'项目费用报销', 'id'=>'zhi_enbao'],
                [ 'title'=>'项目支出台账-自营', 'id'=>'tai_zhi_zi','business'=>1],
                [ 'title'=>'项目支出台账-联营', 'id'=>'tai_zhi_lian','business'=>2],
            ]],
            ['id'=>'cheng', 'name'=>'成本管理', 'down'=>[
                [ 'title'=>'预算编制', 'id'=>'engin_budget'],
                [ 'title'=>'预算执行分析', 'id'=>'tai_yu_fen'],
                [ 'title'=>'成本构成分析表', 'id'=>'tai_cheng_fen'],
                [ 'title'=>'进项票构成分析表', 'id'=>'tai_piao_fen'],
            ]],
            ['id'=>'rcj', 'name'=>'人材机管理', 'down'=>[
                [ 'title'=>'材料入库', 'id'=>'stuff_ru'],
                [ 'title'=>'材料出库', 'id'=>'stuff_chu'],
                [ 'title'=>'材料退库', 'id'=>'stuff_tui'],
                [ 'title'=>'材料库存', 'id'=>'stuff_ku'],
                [ 'title'=>'设备进场', 'id'=>'device_jin'],
                [ 'title'=>'设备使用', 'id'=>'device_mingxi'],
                [ 'title'=>'工人花名册', 'id'=>'labour_gong'],
                [ 'title'=>'项目花名册', 'id'=>'labour_xiang'],
                [ 'title'=>'用工单', 'id'=>'labour_yong'],
            ]],
            ['id'=>'jindu', 'name'=>'进度管理', 'down'=>[
                [ 'title'=>'工程量清单', 'id'=>'engin_godan'],
                [ 'title'=>'总进度计划', 'id'=>'speed_zong'],
                [ 'title'=>'月进度计划', 'id'=>'speed_month'],
                [ 'title'=>'施工日志', 'id'=>'engin_diary'],
                [ 'title'=>'进度填报', 'id'=>'engin_speed'],
                [ 'title'=>'月进度跟踪', 'id'=>'speed_jingen'],
                [ 'title'=>'项目进度汇总', 'id'=>'speed_jinhui'],
            ]],
            ['id'=>'xunzheng', 'name'=>'巡检管理', 'down'=>[
                [ 'title'=>'项目巡检', 'id'=>'engin_xun'],
                [ 'title'=>'整改通知', 'id'=>'engin_zheng'],
            ]],
            ['id'=>'xiangzi', 'name'=>'项目资料', 'down'=>[
                [ 'title'=>'项目资料盖章', 'id'=>'engin_stamp'],
                [ 'title'=>'项目资料库', 'id'=>'engin_fold'],
            ]],
            ['id'=>'bond', 'name'=>'保证金', 'down'=>[
                [ 'title'=>'保证金收取', 'id'=>'bond_shou'],
                [ 'title'=>'保证金缴纳', 'id'=>'bond_jiao'],
                [ 'title'=>'保证金退回', 'id'=>'bond_tui'],
                [ 'title'=>'保证金返还', 'id'=>'bond_fan'],
                [ 'title'=>'保函开具', 'id'=>'bond_kai'],
                [ 'title'=>'项目保证金台账-自营', 'id'=>'tai_bond_zi','business'=>1],
                [ 'title'=>'项目保证金台账-联营', 'id'=>'tai_bond_lian','business'=>2],
            ]],
            ['id'=>'lian', 'name'=>'联营管理','business'=>2, 'down'=>[
                [ 'title'=>'联营合同', 'id'=>'contract_lian'],
                [ 'title'=>'应扣费用表', 'id'=>'lian_yingkou'],
                [ 'title'=>'费用扣收一览', 'id'=>'lian_koulan'],
                [ 'title'=>'进项退税', 'id'=>'zhi_jintui'],
                [ 'title'=>'联营结算', 'id'=>'zhi_lianjie'],
                [ 'title'=>'联营项目扣收台账', 'id'=>'tai_kou_lian'],
                [ 'title'=>'项目资金台账', 'id'=>'lian_zitai'],
            ]],
        ];

        $userMenuStr = (new MenuService())->get_user_authmenu();
        foreach ($authlanbut as $k=>$v){
            if(isset($v['business'])){
                if($data['business'] != $v['business']){
                    unset($authlanbut[$k]);
                }
            }

            foreach ($v['down'] as $kk=>$vv){
                if(isset($vv['business'])){
                    if($data['business'] != $vv['business']){
                        unset($authlanbut[$k]['down'][$kk]);
                    }
                }
                $menu = MenuModel::where(['modedir'=>$vv['id'],'status'=>1])->find();
                if(!$menu){
                    unset($authlanbut[$k]['down'][$kk]);
                }else{
                    if($this->userinfo['issuper'] < 1 && !in_array($menu['id'], explode(',', $userMenuStr))){
                        unset($authlanbut[$k]['down'][$kk]);
                    }
                }
            }
        }


        $data['authlanbut'] = $authlanbut;
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_fen_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $yuexianshouzhimonth = $request->post('yuexianshouzhimonth', '', 'string');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $data['xiangmaofee'] = round($data['balancefee'] - $data['shougongdianfee'], 2);
        $data['xiangmaorate'] = 0;
        if($data['shouxiangfee'] - $data['shougongdianfee'] != 0){
            $data['xiangmaorate'] = round($data['xiangmaofee']/($data['shouxiangfee'] - $data['shougongdianfee'])*100, 2);
        }

        $yuexianshouzhi = [];
        if($yuexianshouzhimonth){
            $monthArr = explode(' - ', $yuexianshouzhimonth);
            $quanMonthArr = getMonthsInRange($monthArr[0], $monthArr[1]);
            foreach ($quanMonthArr as $month){
                $sea_year = date("Y", strtotime($month));
                $sea_month = date("m", strtotime($month));

                $shoufee = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid'],'kuan.enginid'=>$enginid])
                    ->where('list.modtype','<',20)
                    ->where(Db::raw("YEAR(list.dt)=".$sea_year." and MONTH(list.dt)=".$sea_month))->sum('list.money');

                $zhifee = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid'],'kuan.enginid'=>$enginid])
                    ->where('list.modtype','>',20)
                    ->where(Db::raw("YEAR(list.dt)=".$sea_year." and MONTH(list.dt)=".$sea_month))->sum('list.money');

                $yuexianshouzhi[] = [$month, $shoufee,$zhifee];
            }
        }
        $data['yuexianshouzhi'] = $yuexianshouzhi;

        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');
        $wlk_bzj_lybzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_lybzj');
        $wlk_bzj_nmggzbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_nmggzbzj');

        $data['bond_tbbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
        $data['bond_lybzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_lybzj])->sum('money');
        $data['bond_nmggzbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_nmggzbzj])->sum('money');

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_cheng_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $data['zongfee'] = round($data['zhixiangfee'] + $data['zhienbaofee'], 2);
        $data['budget_yufee'] = round($data['budget_money'] - $data['zongfee'], 2);
        $data['budget_zongyurate'] = 0;
        if($data['budget_money'] != 0){
            $data['budget_zongyurate'] = round($data['zongfee']/$data['budget_money']*100, 2);
        }


        $data['rate_cai'] = 0;
        $data['rate_ren'] = 0;
        $data['rate_ji'] = 0;
        $data['rate_fen'] = 0;
        $data['rate_qi'] = 0;
        if($data['budget_caifee'] != 0){
            $data['rate_cai'] = round($data['zhixiang_cai'] / $data['budget_caifee'] *100, 2);
        }
        if($data['budget_rate_ren'] != 0){
            $data['rate_ren'] = round($data['zhixiang_ren'] / $data['budget_rate_ren'] *100, 2);
        }
        if($data['budget_rate_ji'] != 0){
            $data['rate_ji'] = round($data['zhixiang_ji'] / $data['budget_rate_ji'] *100, 2);
        }
        if($data['budget_rate_fen'] != 0){
            $data['rate_fen'] = round($data['zhixiang_fen'] / $data['budget_rate_fen'] *100, 2);
        }

        $data['zhixiang_qi_all'] = round($data['zhixiang_qi'] + $data['zhienbaofee'], 2);
        if($data['budget_rate_qi'] != 0){
            $data['rate_qi'] = round($data['zhixiang_qi_all'] / $data['budget_rate_qi'] *100, 2);
        }

        $data['yufee_cai'] = round($data['budget_caifee'] - $data['zhixiang_cai'], 2);
        $data['yufee_ren'] = round($data['budget_rate_ren'] - $data['zhixiang_ren'], 2);
        $data['yufee_ji'] = round($data['budget_rate_ji'] - $data['zhixiang_ji'], 2);
        $data['yufee_fen'] = round($data['budget_rate_fen'] - $data['zhixiang_fen'], 2);
        $data['yufee_qi'] = round($data['budget_rate_qi'] - $data['zhixiang_qi_all'], 2);

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_jin_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['zong_wanbi'] = 0;
        $data['zong_chanfee'] = 0;
        $data['zong_wanchan'] = 0;
        $data['zong_startdt'] = '';
        $data['zong_enddt'] = '';
        $data['zong_shijitian'] = 0;
        $data['zong_jinbi'] = 0;
        $speedZong = SpeedZongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id']])->find();
        if($speedZong){
            $data['zong_chanfee'] = $speedZong['chanfee'];
            $data['zong_startdt'] = $speedZong['startdt'];
            $data['zong_enddt'] = $speedZong['enddt'];
            $speedZong['findt'] = '';

            $zongList = SpeedZongListModel::where(['zongid'=>$speedZong['id']])->select()->toArray();
            foreach ($zongList as $v){
                $findt = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->value('list.findt');
                if(!empty($findt)){
                    if(empty($speedZong['findt'])){
                        $speedZong['findt'] = $findt;
                    }else{
                        if($findt <= $speedZong['findt']){
                            $speedZong['findt'] = $findt;
                        }
                    }
                }
                $shinum = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->sum('list.shinum');
                $data['zong_wanchan'] += $shinum*$v['price'];
            }

            if($speedZong['chanfee'] != 0){
                $data['zong_wanbi'] = round($data['zong_wanchan'] / $speedZong['chanfee'] *100, 2);
            }

            if(!empty($speedZong['findt']) && $speedZong['findt'] != '0000-00-00'){
                $jihua = get_time_slot_days($speedZong['startdt'], $speedZong['enddt']);
                $data['zong_shijitian'] = get_time_slot_days($speedZong['findt'], date("Y-m-d"));
                if($jihua != 0){
                    $data['zong_jinbi'] = round($data['zong_shijitian']/$jihua*100, 2);
                }
            }
            if(!$data['zong_jinbi']){
                $data['zong_jinbi'] = 0;
            }
        }

        $data['zong_jinyu'] = '正常';
        if($data['zong_wanbi'] < $data['zong_jinbi']){
            $data['zong_jinyu'] = '滞后';
        }


        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_piao_engin_cha(Request $request){
        $yueshoukaimonth = $request->post('yueshoukaimonth', '', 'string');
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['shou_nofee_zhuan'] = round($data['shou_money'] - $data['shou_ratefee_zhuan'], 2);

        $yuekaishou = [];
        if($yueshoukaimonth){
            $monthArr = explode(' - ', $yueshoukaimonth);
            $quanMonthArr = getMonthsInRange($monthArr[0], $monthArr[1]);
            foreach ($quanMonthArr as $month){
                $sea_year = date("Y", strtotime($month));
                $sea_month = date("m", strtotime($month));
                $kaifee = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $shoufee = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $yuekaishou[] = [$month, $kaifee,$shoufee];
            }
        }
        $data['yuekaishou'] = $yuekaishou;

        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');
        $wlk_bzj_lybzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_lybzj');
        $wlk_bzj_nmggzbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_nmggzbzj');

        $data['bond_tbbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
        $data['bond_lybzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_lybzj])->sum('money');
        $data['bond_nmggzbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_nmggzbzj])->sum('money');

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_ren_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['stuff'] = StuffRuListModel::alias('list')
            ->field('sum(list.money) as listrumoney
            ,sum(list.chumoney) as listchumoney
            ,sum(list.tuimoney) as listtuimoney
            ,sum(list.cunmoney) as listcunmoney
            ,sum(list.yongmoney) as listyongmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
            ->where(['ru.isturn'=>1,'ru.status'=>1,'ru.enginid'=>$data['id']])
            ->find();

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function zi_xun_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['zheng_all'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id']])->count();
        $data['zheng_um'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>1])->count();
        $data['zheng_no'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>0])->count();
        $data['zheng_yu'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>0])->where('dt', '<', date("Y-m-d"))->count();

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_fen_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $yuedushouzhimonth = $request->post('yuedushouzhimonth', '', 'string');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }


        $yuexianshouzhi = [];
        if($yuedushouzhimonth){
            $monthArr = explode(' - ', $yuedushouzhimonth);
            $quanMonthArr = getMonthsInRange($monthArr[0], $monthArr[1]);
            foreach ($quanMonthArr as $month){
                $sea_year = date("Y", strtotime($month));
                $sea_month = date("m", strtotime($month));

                $shoufee = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid'],'kuan.enginid'=>$enginid])
                    ->where('list.modtype','<',20)
                    ->where(Db::raw("YEAR(list.dt)=".$sea_year." and MONTH(list.dt)=".$sea_month))->sum('list.money');

                $zhifee = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid'],'kuan.enginid'=>$enginid])
                    ->where('list.modtype','>',20)
                    ->where(Db::raw("YEAR(list.dt)=".$sea_year." and MONTH(list.dt)=".$sea_month))->sum('list.money');
                $yuexianshouzhi[] = [$month, $shoufee,$zhifee];
            }
        }
        $data['yuexianshouzhi'] = $yuexianshouzhi;

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_cheng_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $data['zongfee'] = round($data['zhixiangfee'] + $data['zhienbaofee'], 2);
        $data['budget_yufee'] = round($data['budget_money'] - $data['zongfee'], 2);
        $data['budget_zongyurate'] = 0;
        if($data['budget_money'] != 0){
            $data['budget_zongyurate'] = round($data['zongfee']/$data['budget_money']*100, 2);
        }


        $data['rate_cai'] = 0;
        $data['rate_ren'] = 0;
        $data['rate_ji'] = 0;
        $data['rate_fen'] = 0;
        $data['rate_qi'] = 0;
        if($data['budget_caifee'] != 0){
            $data['rate_cai'] = round($data['zhixiang_cai'] / $data['budget_caifee'] *100, 2);
        }
        if($data['budget_rate_ren'] != 0){
            $data['rate_ren'] = round($data['zhixiang_ren'] / $data['budget_rate_ren'] *100, 2);
        }
        if($data['budget_rate_ji'] != 0){
            $data['rate_ji'] = round($data['zhixiang_ji'] / $data['budget_rate_ji'] *100, 2);
        }
        if($data['budget_rate_fen'] != 0){
            $data['rate_fen'] = round($data['zhixiang_fen'] / $data['budget_rate_fen'] *100, 2);
        }

        $data['zhixiang_qi_all'] = round($data['zhixiang_qi'] + $data['zhienbaofee'], 2);
        if($data['budget_rate_qi'] != 0){
            $data['rate_qi'] = round($data['zhixiang_qi_all'] / $data['budget_rate_qi'] *100, 2);
        }

        $data['yufee_cai'] = round($data['budget_caifee'] - $data['zhixiang_cai'], 2);
        $data['yufee_ren'] = round($data['budget_rate_ren'] - $data['zhixiang_ren'], 2);
        $data['yufee_ji'] = round($data['budget_rate_ji'] - $data['zhixiang_ji'], 2);
        $data['yufee_fen'] = round($data['budget_rate_fen'] - $data['zhixiang_fen'], 2);
        $data['yufee_qi'] = round($data['budget_rate_qi'] - $data['zhixiang_qi_all'], 2);

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_jin_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['zong_wanbi'] = 0;
        $data['zong_chanfee'] = 0;
        $data['zong_wanchan'] = 0;
        $data['zong_startdt'] = '';
        $data['zong_enddt'] = '';
        $data['zong_shijitian'] = 0;
        $data['zong_jinbi'] = 0;
        $speedZong = SpeedZongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id']])->find();
        if($speedZong){
            $data['zong_chanfee'] = $speedZong['chanfee'];
            $data['zong_startdt'] = $speedZong['startdt'];
            $data['zong_enddt'] = $speedZong['enddt'];
            $speedZong['findt'] = '';

            $zongList = SpeedZongListModel::where(['zongid'=>$speedZong['id']])->select()->toArray();
            foreach ($zongList as $v){
                $findt = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->value('list.findt');
                if(!empty($findt)){
                    if(empty($speedZong['findt'])){
                        $speedZong['findt'] = $findt;
                    }else{
                        if($findt <= $speedZong['findt']){
                            $speedZong['findt'] = $findt;
                        }
                    }
                }
                $shinum = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$speedZong['enginid'],'list.zongid'=>$v['id']])
                    ->sum('list.shinum');
                $data['zong_wanchan'] += $shinum*$v['price'];
            }

            if($speedZong['chanfee'] != 0){
                $data['zong_wanbi'] = round($data['zong_wanchan'] / $speedZong['chanfee'] *100, 2);
            }

            if(!empty($speedZong['findt']) && $speedZong['findt'] != '0000-00-00'){
                $jihua = get_time_slot_days($speedZong['startdt'], $speedZong['enddt']);
                $data['zong_shijitian'] = get_time_slot_days($speedZong['findt'], date("Y-m-d"));
                if($jihua != 0){
                    $data['zong_jinbi'] = round($data['zong_shijitian']/$jihua*100, 2);
                }
            }
            if(!$data['zong_jinbi']){
                $data['zong_jinbi'] = 0;
            }
        }

        $data['zong_jinyu'] = '正常';
        if($data['zong_wanbi'] < $data['zong_jinbi']){
            $data['zong_jinyu'] = '滞后';
        }

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_piao_engin_cha(Request $request){
        $yueshoukaimonth = $request->post('yueshoukaimonth', '', 'string');
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['shou_nofee_zhuan'] = round($data['shou_money'] - $data['shou_ratefee_zhuan'], 2);

        $yuekaishou = [];
        if($yueshoukaimonth){
            $monthArr = explode(' - ', $yueshoukaimonth);
            $quanMonthArr = getMonthsInRange($monthArr[0], $monthArr[1]);
            foreach ($quanMonthArr as $month){
                $sea_year = date("Y", strtotime($month));
                $sea_month = date("m", strtotime($month));
                $kaifee = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $shoufee = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $yuekaishou[] = [$month, $kaifee,$shoufee];
            }
        }
        $data['yuekaishou'] = $yuekaishou;

        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');
        $wlk_bzj_lybzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_lybzj');
        $wlk_bzj_nmggzbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_nmggzbzj');

        $data['bond_tbbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
        $data['bond_lybzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_lybzj])->sum('money');
        $data['bond_nmggzbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_nmggzbzj])->sum('money');

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_fei_engin_cha(Request $request){
        $yueshoukaimonth = $request->post('yueshoukaimonth', '', 'string');
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['shou_nofee_zhuan'] = round($data['shou_money'] - $data['shou_ratefee_zhuan'], 2);

        $yuekaishou = [];
        if($yueshoukaimonth){
            $monthArr = explode(' - ', $yueshoukaimonth);
            $quanMonthArr = getMonthsInRange($monthArr[0], $monthArr[1]);
            foreach ($quanMonthArr as $month){
                $sea_year = date("Y", strtotime($month));
                $sea_month = date("m", strtotime($month));
                $kaifee = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $shoufee = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$data['id']])
                    ->where(Db::raw("YEAR(dt)=".$sea_year." and MONTH(dt)=".$sea_month))->sum('money');
                $yuekaishou[] = [$month, $kaifee,$shoufee];
            }
        }
        $data['yuekaishou'] = $yuekaishou;

        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');
        $wlk_bzj_lybzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_lybzj');
        $wlk_bzj_nmggzbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_nmggzbzj');

        $data['bond_tbbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
        $data['bond_lybzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_lybzj])->sum('money');
        $data['bond_nmggzbzjfee'] = BondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$data['id'],'feitype'=>$wlk_bzj_nmggzbzj])->sum('money');

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_ren_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['stuff'] = StuffRuListModel::alias('list')
            ->field('sum(list.money) as listrumoney
            ,sum(list.chumoney) as listchumoney
            ,sum(list.tuimoney) as listtuimoney
            ,sum(list.cunmoney) as listcunmoney
            ,sum(list.yongmoney) as listyongmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
            ->where(['ru.isturn'=>1,'ru.status'=>1,'ru.enginid'=>$data['id']])
            ->find();

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function lian_xun_engin_cha(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $data = EnginModel::get_info_by_pk($enginid);
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['zheng_all'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id']])->count();
        $data['zheng_um'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>1])->count();
        $data['zheng_no'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>0])->count();
        $data['zheng_yu'] = EnginZhengModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['id'],'gaista'=>0])->where('dt', '<', date("Y-m-d"))->count();
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function detail_engin_budget(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['bud.deleted_user','=', 0];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','in',[0,1]];

        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bud.enginid','=', $param['enginid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bud.status','=', $param['status']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bill.sericnum|bud.flowuser|list.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bud.flowuser','like', '%'.$param['flowuser'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['bud.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['budtype']) && !empty($param['budtype'])){
            $where[] = ['budtype','=', $param['budtype']];
        }

        $data['budtype'] = (new EnginService())->get_engin_budget_budtype();
        $data['is_budtype'] = isset($param['budtype']) ? $param['budtype'] : '';


        $data['data'] = EnginBudgetListModel::alias('list')
            ->field('bud.*,e.name as enginname,e.chanid as engin_chanid,bill.sericnum,
            list.id as listid,list.budtype,list.name as listname,list.unit as listunit,list.num as listnum
            ,list.price as listprice,list.money as listmoney,list.explain as listexplain')
            ->join('engin_budget bud','bud.id=list.budgetid')
            ->join('flow_bill bill','bill.id=bud.billid')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_budget', 'bud'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['engin_chanid_name'] = DeptModel::get_name_by_pk($item['engin_chanid']);
                $item['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item['budtype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['yiqiannum'] = ContractCaiListModel::alias('list')->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'list.listid'=>$item['listid']])
                    ->sum('list.num');
                $item['yiqianmoney'] = ContractCaiListModel::alias('list')->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'list.listid'=>$item['listid']])
                    ->sum('list.money');
                $item['yiqianjun'] = 0;
                if($item['yiqiannum'] != 0){
                    $item['yiqianjun'] = round($item['yiqianmoney']/$item['yiqiannum'], 2);
                }
                $item['shengnum'] = round($item['listnum']-$item['yiqiannum'], 2);
                $item['shengmoney'] = round($item['listmoney']-$item['yiqianmoney'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_budget(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=','0'];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bud.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['bianer']) && !empty($param['bianer'])){
            $where[] = ['bud.bianer','like', '%'.$param['bianer'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['bud.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bud.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginBudgetModel::alias('bud')
            ->field('bud.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=bud.enginid')
            ->leftJoin('flow_bill bill','bill.id=bud.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_budget', 'bud'))
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_budget');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_budget(Request $request){
        $param = $request->post();
        $data = EnginBudgetModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginBudgetModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_budget', $data['id'], '预算编制');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_budget(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBudgetModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginBudgetListModel::where(['budgetid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item['budtype']);
        }
        $data['list'] = $list;

        $bian = EnginBudbianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'budgetid'=>$data['data']['id']])
            ->where('status','in',[0,1])->select()->toArray();
        foreach ($bian as &$sss){
            $sss['type_name'] = (new EnginService())->get_engin_budget_type_name($sss['type']);
        }

        $data['bian'] = $bian;

        $data['files'] = (new OptService())->get_file('engin_budget', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_budbian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=','0'];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','in',[0,1]];


        if(isset($param['budgetid']) && !empty($param['budgetid'])){
            $where[] = ['bud.budgetid','=', $param['budgetid']];
        }

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bud.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bud.number|bud.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['bud.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginBudbianModel::alias('bud')
            ->field('bud.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=bud.enginid')
            ->leftJoin('flow_bill bill','bill.id=bud.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_budbian', 'bud'))
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['type_name'] = (new EnginService())->get_engin_budget_type_name($item['type']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_budbian');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_budbian(Request $request){
        $param = $request->post();
        $data = EnginBudbianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginBudbianModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_budbian', $data['id'], '预算变更');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_budbian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBudbianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['budgetinfo'] = EnginBudgetModel::where(['id'=>$data['data']['budgetid']])->find();

        $data['data']['type_name'] = (new EnginService())->get_engin_budget_type_name($data['data']['type']);

        $tiaolist = EnginBudbianListModel::where(['budbianid'=>$data['data']['id']])->where('listid','gt',0)->select()->toArray();
        foreach ($tiaolist as &$item_tiao){
            $item_tiao['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item_tiao['budtype']);
        }
        $data['tiaolist'] = $tiaolist;

        $zenglist = EnginBudbianListModel::where(['budbianid'=>$data['data']['id']])->where('listid','=',0)->select()->toArray();
        foreach ($zenglist as &$item_zeng){
            $item_zeng['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item_zeng['budtype']);
        }
        $data['zenglist'] = $zenglist;







        $data['files'] = (new OptService())->get_file('engin_budbian', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_godan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['godan.deleted_user','=', 0];
        $where[] = ['godan.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['godan.enginid','=', $param['enginid']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginGodanModel::alias('godan')
            ->field('godan.*,e.name as enginname')
            ->join('engin e','e.id=godan.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_godan', 'godan'))
            ->order('godan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_godan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_godan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            $exist = EnginGodanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$param['enginid']])->find();
            if($exist){
                return outputJson(4000, '该项目已存在工程量清单,请重新选择');
            }

            $param['number'] = (new BianhaoService())->create_engin_godan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_godan') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入清单明细');
            }

            Db::startTrans();
            try {
                $id = EnginGodanModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['godanid'] = $id;
                    EnginGodanListModel::insert($vv);
                }

                (new EnginRelevan())->vate_engin_godan_list($id);

                (new OptService())->relation_file($files,'engin_godan',$id);
                (new EnginRelevan())->vate_engin_file('engin_godan', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_godan', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_godan', $id, '工程量清单');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new BianhaoService())->create_engin_godan_number();
            $data['files'] = (new OptService())->get_file('engin_godan');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_godan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_godan') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入清单明细');
            }

            Db::startTrans();
            try {
                EnginGodanModel::update($param);

                EnginGodanListModel::where(['godanid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['godanid'] = $param['id'];
                    EnginGodanListModel::insert($vv);
                }

                (new EnginRelevan())->vate_engin_godan_list($param['id']);

                (new OptService())->relation_file($files,'engin_godan',$param['id']);
                (new EnginRelevan())->vate_engin_file('engin_godan', $param['id']);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_godan', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_godan', $param['id'], '工程量清单');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginGodanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['list'] = EnginGodanListModel::where(['godanid'=>$data['data']['id']])->select()->toArray();
            $data['files'] = (new OptService())->get_file('engin_godan', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_godan(Request $request){
        $param = $request->post();
        $data = EnginGodanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            EnginGodanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new EnginRelevan())->vate_engin_godan_list($data['id']);
            (new EnginRelevan())->vate_engin_file('engin_godan', $data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_godan', $data['id'], '工程量清单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_godan(Request $request){
        $param = $request->post();
        $data['data'] = EnginGodanModel::where(['id'=>$param['id']])->find();
        if(!$data['data']){
            return outputJson(2000,lang('code_2000'),[]);
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['list'] = EnginGodanListModel::where(['godanid'=>$data['data']['id']])->select()->toArray();
        $data['files'] = (new OptService())->get_file('engin_godan', $data['data']['id']);
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bond_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['hantype']) && !empty($param['hantype'])){
            $where[] = ['kai.hantype','=', $param['hantype']];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['kai.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['beidaner']) && !empty($param['beidaner'])){
            $where[] = ['kai.beidaner','like', '%'.$param['beidaner'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['kai.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['kai.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['xiaodt']) && !empty($param['xiaodt'])){
            $where[] = ['kai.xiaodt','between', explode(' - ', $param['xiaodt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');
        $data['is_hantype'] = isset($param['hantype']) ? $param['hantype'] : '';


        $data['data'] = BondKaiModel::alias('kai')
            ->field('kai.*,e.name as enginname,bill.sericnum,bill.modename')
            ->leftJoin('engin e','e.id=kai.enginid')
            ->leftJoin('flow_bill bill','bill.id=kai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bond_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hantype_name'] = (new OptService())->get_base_showname('engin_bond_hantype', $item['hantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bond_kai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function kai_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['kaidt'])){
                return outputJson(4000, '请选择开具日期');
            }
            if(empty($param['xiaodt'])){
                return outputJson(4000, '请选择有效期');
            }

            $param['state'] = 1;
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'bond_kai') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                BondKaiModel::update($param);
                (new OptService())->relation_file($files,'bond_kai',$param['id']);
                (new OptService())->add_flow_log('开具', 'bond_kai', $param['id']);
                (new OptService())->add_log_list('开具', 'bond_kai', $param['id'], '项目保函开具');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondKaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['data']['hantype_name'] = (new OptService())->get_base_showname('engin_bond_hantype', $data['data']['hantype']);
            $data['files'] = (new OptService())->get_file('bond_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_bond_kai(Request $request){
        $param = $request->post();
        $data = BondKaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            BondKaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bond_kai', $data['id'], '项目保函开具');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bond_kai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BondKaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['state_name'] = $data['data']['state'] > 0 ? '已开具' : '未开具';
        $data['data']['hantype_name'] = (new OptService())->get_base_showname('engin_bond_hantype', $data['data']['hantype']);
        $data['files'] = (new OptService())->get_file('bond_kai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function detail_dai_kou_qi(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['dai.deleted_user','=', 0];
        $where[] = ['dai.comid','=',$this->userinfo['comid']];
        $where[] = ['dai.isturn','=',1];
        $where[] = ['dai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['dai.enginid','=', $param['enginid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['dai.status','=', $param['status']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dai.zhai|dai.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listexplain']) && !empty($param['listexplain'])){
            $where[] = ['list.explain','like', '%'.$param['listexplain'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['dai.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['dai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['dai.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['list.feitype','=', $param['feitype']];
        }

        if(isset($param['state']) && !empty($param['state'])){
            if($param['state'] == 1){
                $where[] = ['list.kouid','<', 1];
            }elseif($param['state'] == 2){
                $where[] = ['list.kouid','>', 0];
            }
        }

        $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = DaiKouQiListModel::alias('list')
            ->field('dai.*,e.name as enginname,bill.sericnum,list.kouid,
            list.feitype,list.money as listmoney,list.explain as listexplain')
            ->join('dai_kou_qi dai','dai.id=list.kouqiid')
            ->join('engin e','e.id=dai.enginid')
            ->leftJoin('flow_bill bill','bill.id=dai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dai_kou_qi', 'dai'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['state_name'] = $item['kouid'] > 0 ? '已扣收' : '待扣收';
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_dai_kou_qi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dai.deleted_user','=','0'];
        $where[] = ['dai.comid','=',$this->userinfo['comid']];
        $where[] = ['dai.isturn','=',1];
        $where[] = ['dai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['dai.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['dai.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['dai.status','=', $param['status']];
        }

        $data['data'] = DaiKouQiModel::alias('dai')
            ->field('dai.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=dai.enginid')
            ->leftJoin('flow_bill bill','bill.id=dai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dai_kou_qi', 'dai'))
            ->order('dai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dai_kou_qi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_dai_kou_qi(Request $request){
        $param = $request->post();
        $data = DaiKouQiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DaiKouQiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dai_kou_qi', $data['id'], '其他费待扣登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_dai_kou_qi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DaiKouQiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = DaiKouQiListModel::where(['kouqiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('dai_kou_qi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_lan_bond_tou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');

        $neiWhere = [];
        $neiWhere[] = ['deleted_user','=', 0];
        $neiWhere[] = ['comid','=',$this->userinfo['comid']];
        $neiWhere[] = ['isturn','=',1];
        $neiWhere[] = ['status','=',1];

        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.number,e.business,e.state,e.status,e.chanid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($wlk_bzj_tbbzj, $neiWhere){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['feitype'] = $wlk_bzj_tbbzj;
                $item['feitype_name'] = '投标保证金';

                $item['bondshoufee'] = BondShouModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondjiaofee'] = BondJiaoModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondtuifee'] = BondTuiModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondtuikoufee'] = BondTuiModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('koufee');
                $item['bondfanfee'] = BondFanModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bonddaituifee'] = round($item['bondjiaofee'] - $item['bondtuifee'] - $item['bondtuikoufee'], 2);

                $item['bonddaifanfee'] = round($item['bondshoufee'] - $item['bondjiaofee'] + $item['bondtuifee'] - $item['bondfanfee'], 2);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_lan_bond_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';




        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.number,e.business,e.state,e.status,e.chanid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                return $item;
            })->toArray();




        $neiWhere = [];
        $neiWhere[] = ['deleted_user','=', 0];
        $neiWhere[] = ['comid','=',$this->userinfo['comid']];
        $neiWhere[] = ['isturn','=',1];
        $neiWhere[] = ['status','=',1];

        $wlk_bzj_lybzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_lybzj');
        $wlk_bzj_nmggzbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_nmggzbzj');
        $baoType = [
           ['feitype'=>$wlk_bzj_lybzj, 'feitype_name'=>'履约保证金'],
           ['feitype'=>$wlk_bzj_nmggzbzj, 'feitype_name'=>'农民工工资保证金'],
        ];

        $newData = [];
        foreach ($data['data']['data'] as $item){
            foreach ($baoType as $vvv){
                $arr = $item;
                $arr['feitype'] = $vvv['feitype'];
                $arr['feitype_name'] = $vvv['feitype_name'];

                $arr['bondshoufee'] = BondShouModel::where($neiWhere)->where(['enginid'=>$arr['id'],'feitype'=>$vvv['feitype']])->sum('money');
                $arr['bondjiaofee'] = BondJiaoModel::where($neiWhere)->where(['enginid'=>$arr['id'],'feitype'=>$vvv['feitype']])->sum('money');
                $arr['bondtuifee'] = BondTuiModel::where($neiWhere)->where(['enginid'=>$arr['id'],'feitype'=>$vvv['feitype']])->sum('money');
                $arr['bondtuikoufee'] = BondTuiModel::where($neiWhere)->where(['enginid'=>$arr['id'],'feitype'=>$vvv['feitype']])->sum('koufee');
                $arr['bondfanfee'] = BondFanModel::where($neiWhere)->where(['enginid'=>$arr['id'],'feitype'=>$vvv['feitype']])->sum('money');
                $arr['bonddaituifee'] = round($arr['bondjiaofee'] - $arr['bondtuifee'] - $arr['bondtuikoufee'], 2);
                $arr['bonddaifanfee'] = round($arr['bondshoufee'] - $arr['bondjiaofee'] + $arr['bondtuifee'] - $arr['bondfanfee'], 2);
                $newData[] = $arr;
            }
        }

        $data['data']['data'] = $newData;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_lan_tou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $wlk_bzj_tbbzj = (new UserService())->get_optionfeiid_bynum('wlk_bzj_tbbzj');

        $neiWhere = [];
        $neiWhere[] = ['deleted_user','=', 0];
        $neiWhere[] = ['comid','=',$this->userinfo['comid']];
        $neiWhere[] = ['isturn','=',1];
        $neiWhere[] = ['status','=',1];

        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.state,e.status,e.chanid
            ,e.shoutoufee,e.zhitoufee,(e.shoutoufee - e.zhitoufee) as shouzhicha')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($neiWhere, $wlk_bzj_tbbzj){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['bondshoufee'] = BondShouModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondjiaofee'] = BondJiaoModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondtuifee'] = BondTuiModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondfanfee'] = BondFanModel::where($neiWhere)->where(['enginid'=>$item['id'],'feitype'=>$wlk_bzj_tbbzj])->sum('money');
                $item['bondyufee'] = round($item['bondshoufee'] - $item['bondjiaofee'] + $item['bondtuifee'] - $item['bondfanfee'], 2);
                $item['lirun'] = round($item['shoutoufee'] - $item['zhitoufee'] + $item['bondshoufee'] - $item['bondjiaofee'] + $item['bondtuifee'] - $item['bondfanfee'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_yingkou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.lian_unit','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.chanid,e.lian_unit,e.shougonghuifee,e.kai_money
            ,e.lian_guanrate,e.lian_guanfee,e.kou_guanfee,e.dai_guanfee
            ,e.lian_shuirate,e.lian_shuifee,e.kou_shuifee,e.dai_shuifee
            ,e.kou_qifee,e.daikouqidaifee')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['kou_guanfee'] = $item['kou_guanfee'] > 0 ? $item['kou_guanfee'] : 0;
                $item['kou_shuifee'] = $item['kou_shuifee'] > 0 ? $item['kou_shuifee'] : 0;

                $item['daikouallfee'] = round($item['dai_guanfee'] + $item['dai_shuifee'] + $item['daikouqidaifee'], 2);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_koulan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user|zhi.deleted_user','=', 0];
        $where[] = ['shou.comid|zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn|zhi.isturn','=',1];
        $where[] = ['shou.status|zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kou.enginid','=', $param['enginid']];
        }
        if(isset($param['feitype_option']) && !empty($param['feitype_option'])){
            if($param['feitype_option'] == 'noguannoshui'){
                $guanId = (new UserService())->get_optionfeiid_bynum('xmsr_lyfyhks_glf');
                $shuiId = (new UserService())->get_optionfeiid_bynum('xmsr_lyfyhks_sf');
                $where[] = ['kou.feitype','neq',$guanId];
                $where[] = ['kou.feitype','neq',$shuiId];
            }else{
                $param['feitype'] = (new UserService())->get_optionfeiid_bynum($param['feitype_option']);
            }
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['lai']) && !empty($param['lai'])){
            if($param['lai'] == 'xiangshou'){
                $where[] = ['kou.zhiid','=', 0];
            }
            if($param['lai'] == 'lianjie'){
                $where[] = ['kou.shouid','=', 0];
            }
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status|zhi.status','=',$param['status']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number|zhi.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['kou.feitype','=',$param['feitype']];
        }
        if(isset($param['explain']) && !empty($param['explain'])){
            $where[] = ['kou.explain','like', '%'.$param['explain'].'%'];
        }
        if(isset($param['koudt']) && !empty($param['koudt'])){
            $where[] = ['shou.dt|zhi.jiedt','between', explode(' - ', $param['koudt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = ShouXiangKouModel::alias('kou')
            ->field('kou.enginid,e.name as enginname,e.chanid
            ,kou.id as kouid,kou.shouid,kou.zhiid,kou.enginid,kou.feitype,kou.money,kou.explain
            ,shou.number as number_shou,shou.billid as billid_shou,shou.status as status_shou,shou.dt as koudt_shou
            ,zhi.number as number_zhi,zhi.billid as billid_zhi,zhi.status as status_zhi,zhi.jiedt as koudt_zhi')
            ->leftJoin('shou_xiang shou','shou.id=kou.shouid and kou.shouid>0')
            ->leftJoin('zhi_lianjie zhi','zhi.id=kou.zhiid and kou.zhiid>0')
            ->join('engin e','e.id=kou.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['feilei_name'] = '联营费用（扣收）';
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');

                if($item['shouid'] > 0){
                    $item['lai'] = '项目收款';
                    $item['number'] = $item['number_shou'];
                    $item['billid'] = $item['billid_shou'];
                    $item['status'] = $item['status_shou'];
                    $item['koudt'] = $item['koudt_shou'];
                }else{
                    $item['lai'] = '联营结算';
                    $item['number'] = $item['number_zhi'];
                    $item['billid'] = $item['billid_zhi'];
                    $item['status'] = $item['status_zhi'];
                    $item['koudt'] = $item['koudt_zhi'];
                }


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_zitai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.chanid,e.lian_unit
            ,e.shougonghuifee,e.shoulianbeifee,e.lianyingshoufee
            ,e.zhixiangfee,e.zhilianjiefee,e.zhijintuifee,e.lianyingzhifee
            ,e.kou_allfee,e.balancefee')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function lian_huizong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['ziyao']) && !empty($param['ziyao'])){
            $where[] = ['e.ziyao','like', '%'.$param['ziyao'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['kuang']) && !empty($param['kuang'])){
            $where[] = ['e.kuang','like', '%'.$param['kuang'].'%'];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['e.zhao_unit','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['shikaidt']) && !empty($param['shikaidt'])){
            $where[] = ['e.shikaidt','between', explode(' - ', $param['shikaidt'])];
        }
        if(isset($param['shijundt']) && !empty($param['shijundt'])){
            $where[] = ['e.shijundt','between', explode(' - ', $param['shijundt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name,bill.sericnum,bill.modename')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->leftJoin('flow_bill bill','bill.id=e.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_huizong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['state_name'] = (new EnginService())->get_engin_state_name($data['data']['state'], $data['data']['status']);
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['data']['winunit_name'] = DeptModel::where(['id'=>$data['data']['winunit']])->value('name');
        $data['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'enginid'=>$data['data']['id']])->find();



        $data['gongfulist'] = ContractCaiModel::alias('cai')
            ->field('cai.gonger')
            ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0])
            ->where(['cai.isturn'=>1,'cai.status'=>1])
            ->where(['cai.enginid'=>$data['data']['id']])
            ->order('cai.id DESC')
            ->group('cai.gonger')
            ->select()->toArray();
        foreach ($data['gongfulist'] as &$aaa){
            $aaa['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'gonger'=>$aaa['gonger'],'enginid'=>$data['data']['id']])->sum('money');
            $aaa['caijiemoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'gonger'=>$aaa['gonger'],'enginid'=>$data['data']['id']])->sum('caijiemoney');
            $aaa['shou_money'] =  PiaoShouListModel::alias('list')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
                ->where(['shou.isturn'=>1,'shou.status'=>1,'shou.enginid'=>$data['data']['id']])
                ->where(['list.gonger'=>$aaa['gonger']])
                ->sum('list.money');
            $aaa['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'duier'=>$aaa['gonger'],'enginid'=>$data['data']['id']])->sum('money');
        }



        $xmsr_lyfbyj = (new UserService())->get_optionfeiid_bynum('xmsr_lyfbyj');
        $data['shouxiang'] = ShouXiangModel::alias('shou')
            ->field('shou.*')
            ->join('engin e','e.id=shou.enginid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.isturn'=>1,'shou.status'=>1])
            ->where(['shou.enginid'=>$data['data']['id'],'shou.feilei'=>$xmsr_lyfbyj])
            ->order('shou.id DESC')
            ->select()->toArray();
        foreach ($data['shouxiang'] as &$bbb){
            $bbb['feilei_name'] = OptionFeiModel::where(['id'=>$bbb['feilei']])->value('name');
        }


        $data['zhilianjie'] = ZhiLianjieModel::alias('zhi')
            ->field('zhi.*')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0])
            ->where(['zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['zhi.enginid'=>$data['data']['id']])
            ->order('zhi.id DESC')
            ->select()->toArray();
        foreach ($data['zhilianjie'] as &$ccc){
            $ccc['feilei_name'] = OptionFeiModel::where(['id'=>$ccc['feilei']])->value('name');
        }



        return outputJson(2000,lang('code_2000'), $data);
    }

    public function tai_piao_shui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.chanid,e.business
            ,e.kai_money,e.kai_ratefee,e.shou_money,e.shou_ratefee
            ,(e.kai_ratefee-e.shou_ratefee) as zengallfee,e.piaotaxfee')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['zengallfee'] = $item['zengallfee'] > 0 ? $item['zengallfee'] : 0;
                return $item;
            })->toArray();
        $data['butauth'] = (new MenuService())->get_butauth('tai_piao_shui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_piao_shui_tong_info(Request $request){
        $param = $request->post();

        $where[] = ['zhu.deleted_user','=', 0];
        $where[] = ['zhu.comid','=',$this->userinfo['comid']];
        $where[] = ['zhu.isturn','=',1];
        $where[] = ['zhu.status','=',1];

        $year = $request->param('year', date("Y"), 'int');
        $chanid = $request->param('chanid', (new UserService())->get_user_chanid(), 'int');

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = $chanid;
        $data['is_year'] = $year;


        $data['niankaifee'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $data['niankairatefee'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.ratefee');

        $data['nianshoufee'] =  PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $data['nianshouratefee'] =  PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.piaotype'=>1])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.ratefee');

        $data['nianzengfee'] = round($data['niankairatefee'] - $data['nianshouratefee'], 2);
        $data['nianzengfee'] = $data['nianzengfee'] < 0 ? 0 : $data['nianzengfee'];
        $data['niantaxfee'] =  PiaoTaxModel::alias('zhu')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_tax', 'zhu'))
            ->where(['e.chanid'=>$chanid])
            ->where('zhu.month','between',[$year."-01", $year."-12"])
            ->sum('zhu.money');




        //进项票类型
        $jxplx['zong'] = $data['nianshoufee'];
        $jxplx['zhuan'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.piaotype'=>1])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $jxplx['pu'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.piaotype'=>2])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $data['jxplx'] = $jxplx;


        //进项票费用类别
        $jxpfylb['cai'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.yongtype'=>'cai'])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $jxpfylb['ren'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.yongtype'=>'ren'])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $jxpfylb['ji'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.yongtype'=>'ji'])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $jxpfylb['fen'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.yongtype'=>'fen'])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $jxpfylb['qi'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->join('engin e','e.id=zhu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
            ->where(['e.chanid'=>$chanid,'list.yongtype'=>'qi'])
            ->where('list.dt','between',[$year."-01-01", $year."-12-31"])
            ->sum('list.money');
        $data['jxpfylb'] = $jxpfylb;


        $xmsr_gchk = (new UserService())->get_optionfeiid_bynum('xmsr_gchk');
        //年度开收票趋势（按月）
        //年度开票-回款趋势（按月）
        $data['zhu_kai'] = [];
        $data['zhu_shou'] = [];
        $data['zhu_hui'] = [];
        for ($i=1; $i<=12; $i++){
            $data['zhu_kai'][] = PiaoKaiListModel::alias('list')
                ->join('piao_kai zhu','zhu.id=list.kaiid')
                ->join('engin e','e.id=zhu.enginid')
                ->where($where)
                ->where((new MenuService())->get_miss_where('piao_kai', 'zhu'))
                ->where(['e.chanid'=>$chanid])
                ->where('zhu.dt','between',[$year."-".$i."-01", date("Y-m-t", strtotime($year."-".$i."-01"))])
                ->sum('list.money');

            $data['zhu_shou'][] =  PiaoShouListModel::alias('list')
                ->join('piao_shou zhu','zhu.id=list.shouid')
                ->join('engin e','e.id=zhu.enginid')
                ->where((new MenuService())->get_miss_where('piao_shou', 'zhu'))
                ->where($where)
                ->where(['e.chanid'=>$chanid])
                ->where('list.dt','between',[$year."-".$i."-01", date("Y-m-t", strtotime($year."-".$i."-01"))])
                ->sum('list.money');

            $data['zhu_hui'][] = ShouXiangModel::alias('zhu')
                ->join('engin e','e.id=zhu.enginid')
                ->where((new MenuService())->get_miss_where('shou_xiang', 'zhu'))
                ->where($where)
                ->where(['e.chanid'=>$chanid,'zhu.feilei'=>$xmsr_gchk])
                ->where('zhu.dt','between',[$year."-".$i."-01", date("Y-m-t", strtotime($year."-".$i."-01"))])
                ->sum('zhu.money');
        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_shou_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['huibi_anhe'] = 0;
                if($item['shimoney'] != 0){
                    $item['huibi_anhe'] = round($item['shougonghuifee']/$item['shimoney']*100, 2);
                }

                $item['huibi_ankai'] = 0;
                if($item['kai_money'] != 0){
                    $item['huibi_ankai'] = round($item['shougonghuifee']/$item['kai_money']*100, 2);
                }

                $item['huibi_anjie'] = 0;
                if($item['shijiemoney'] != 0){
                    $item['huibi_anjie'] = round($item['shougonghuifee']/$item['shijiemoney']*100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_shou_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['huibi_anhe'] = 0;
                if($item['shimoney'] != 0){
                    $item['huibi_anhe'] = round($item['shougonghuifee']/$item['shimoney']*100, 2);
                }

                $item['huibi_ankai'] = 0;
                if($item['kai_money'] != 0){
                    $item['huibi_ankai'] = round($item['shougonghuifee']/$item['kai_money']*100, 2);
                }

                $item['huibi_anjie'] = 0;
                if($item['shijiemoney'] != 0){
                    $item['huibi_anjie'] = round($item['shougonghuifee']/$item['shijiemoney']*100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_zhi_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_zhi_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_kou_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_yu_fen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_yu_cheng(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['shichengfee'] = round($item['zhixiangfee']+$item['zhienbaofee'], 2);
                $item['shichengbi_all'] = 0;
                if($item['budget_money'] != 0){
                    $item['shichengbi_all'] = round($item['shichengfee']/$item['budget_money']*100, 2);
                }
                $item['shengyufee_all'] = round($item['budget_money']-$item['shichengfee'], 2);

                $item['zhixiang_qi_all'] = round($item['zhixiang_qi'] + $item['zhienbaofee'], 2);

                $item['shichengbi_cai'] = 0;
                if($item['budget_caifee'] != 0){
                    $item['shichengbi_cai'] = round($item['zhixiang_cai']/$item['budget_caifee']*100, 2);
                }

                $item['shichengbi_ren'] = 0;
                if($item['budget_renfee'] != 0){
                    $item['shichengbi_ren'] = round($item['zhixiang_ren']/$item['budget_renfee']*100, 2);
                }

                $item['shichengbi_ji'] = 0;
                if($item['budget_jifee'] != 0){
                    $item['shichengbi_ji'] = round($item['zhixiang_ji']/$item['budget_jifee']*100, 2);
                }

                $item['shichengbi_fen'] = 0;
                if($item['budget_fenfee'] != 0){
                    $item['shichengbi_fen'] = round($item['zhixiang_fen']/$item['budget_fenfee']*100, 2);
                }

                $item['shichengbi_qi'] = 0;
                if($item['budget_qifee'] != 0){
                    $item['shichengbi_qi'] = round($item['zhixiang_qi_all']/$item['budget_qifee']*100, 2);
                }


                $item['shengyufee_cai'] = round($item['budget_caifee']-$item['zhixiang_cai'], 2);
                $item['shengyufee_ren'] = round($item['budget_renfee']-$item['zhixiang_ren'], 2);
                $item['shengyufee_ji'] = round($item['budget_jifee']-$item['zhixiang_ji'], 2);
                $item['shengyufee_fen'] = round($item['budget_fenfee']-$item['zhixiang_fen'], 2);
                $item['shengyufee_qi'] = round($item['budget_qifee']-$item['zhixiang_qi_all'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_cheng_fen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,(e.zhixiangfee+e.zhienbaofee) as zongfee')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['rate_cai'] = 0;
                $item['rate_ren'] = 0;
                $item['rate_ji'] = 0;
                $item['rate_fen'] = 0;
                $item['rate_qi'] = 0;
                if($item['zongfee'] != 0){
                    $item['rate_cai'] = round($item['zhixiang_cai'] / $item['zongfee'] *100, 2);
                }
                if($item['zongfee'] != 0){
                    $item['rate_ren'] = round($item['zhixiang_ren'] / $item['zongfee'] *100, 2);
                }
                if($item['zongfee'] != 0){
                    $item['rate_ji'] = round($item['zhixiang_ji'] / $item['zongfee'] *100, 2);
                }
                if($item['zongfee'] != 0){
                    $item['rate_fen'] = round($item['zhixiang_fen'] / $item['zongfee'] *100, 2);
                }

                $item['zhixiang_qi_all'] = round($item['zhixiang_qi'] + $item['zhienbaofee'], 2);
                if($item['zongfee'] != 0){
                    $item['rate_qi'] = round($item['zhixiang_qi_all'] / $item['zongfee'] *100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_piao_fen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['rate_cai'] = 0;
                $item['rate_ren'] = 0;
                $item['rate_ji'] = 0;
                $item['rate_fen'] = 0;
                $item['rate_qi'] = 0;
                if($item['shou_money'] != 0){
                    $item['rate_cai'] = round($item['shou_money_cai'] / $item['shou_money'] *100, 2);
                }
                if($item['shou_money'] != 0){
                    $item['rate_ren'] = round($item['shou_money_ren'] / $item['shou_money'] *100, 2);
                }
                if($item['shou_money'] != 0){
                    $item['rate_ji'] = round($item['shou_money_ji'] / $item['shou_money'] *100, 2);
                }
                if($item['shou_money'] != 0){
                    $item['rate_fen'] = round($item['shou_money_fen'] / $item['shou_money'] *100, 2);
                }
                if($item['shou_money'] != 0){
                    $item['rate_qi'] = round($item['shou_money_qi'] / $item['shou_money'] *100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_bond_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,(e.bondjiaofee - e.bondtuifee) as daituifee')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_bond_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_bei_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function tai_bei_ren(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['user.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['jieren_name']) && !empty($param['jieren_name'])){
            $where[] = ['user.realname','like', '%'.$param['jieren_name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $chanid_son = (new UserService())->get_deptsonid($param['chanid']);
            $where[] = ['user.deptid','in', $chanid_son];
        }
        if(isset($param['deptid']) && !empty($param['deptid'])){
            $where[] = ['user.deptid','=', $param['deptid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['deptid'] = (new UserService())->get_deptinfo_all();
        $data['is_deptid'] = isset($param['deptid']) ? $param['deptid'] : '';


        $data['data'] = ZhiJieModel::alias('jie')
            ->field('jie.created_user as jieren,user.deptid as user_deptid,user.realname as jieren_name,dept.name as deptname')
            ->join('user user','jie.created_user=user.id')
            ->join('dept dept','dept.id=user.deptid')
            ->where($where)
            ->order('user.id DESC')
            ->group('jie.created_user')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $userChanid = (new UserService())->get_user_chanid($item['user_deptid']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($userChanid);

                $item['zhijiefee'] = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$item['jieren']])->sum('money');
                $item['zhijiehuanfee'] = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$item['jieren']])->sum('huanfee');
                $item['zhijiedifee'] = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$item['jieren']])->sum('difee');
                $item['zhijieyufee'] = ZhiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$item['jieren']])->sum('yufee');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function shu_li_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,(e.shougonghuifee - e.zhixiangfee - e.zhienbaofee) as shirun')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['shirunrate'] = 0;
                if($item['shougonghuifee'] != 0){
                    $item['shirunrate'] = round($item['shirun']/$item['shougonghuifee']*100, 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_jing_zi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                //产值进度：产值填报金额/收入合同金额含补充+项目签证金额含税
                $item['chanbi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['chanbi'] = round( $item['chanmoney']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //结算占比：收入合同结算金额/收入合同金额含补充+项目签证金额含税
                $item['jiebi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['jiebi'] = round( $item['shijiemoney']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //开票占比：项目开票金额/收入合同金额含补充+项目签证金额含税
                $item['kaibi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['kaibi'] = round( $item['kai_money']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //回款占比：工程回款金额/收入合同金额含补充+项目签证金额含税
                $item['huibi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['huibi'] = round( $item['shougonghuifee']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //支出占比：支出合计/收入合计
                $item['zhichubi'] = 0;
                if( $item['lianyingshoufee'] != 0 ){
                    $item['zhichubi'] = round( $item['lianyingzhifee']/$item['lianyingshoufee'] , 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_jing_lian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                //产值进度：产值填报金额/收入合同金额含补充+项目签证金额含税
                $item['chanbi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['chanbi'] = round( $item['chanmoney']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //结算占比：收入合同结算金额/收入合同金额含补充+项目签证金额含税
                $item['jiebi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['jiebi'] = round( $item['shijiemoney']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //开票占比：项目开票金额/收入合同金额含补充+项目签证金额含税
                $item['kaibi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['kaibi'] = round( $item['kai_money']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //回款占比：工程回款金额/收入合同金额含补充+项目签证金额含税
                $item['huibi'] = 0;
                if( ($item['shimoney']+$item['visamoney']) != 0 ){
                    $item['huibi'] = round( $item['shougonghuifee']/($item['shimoney']+$item['visamoney']) , 2);
                }
                //支出占比：支出合计/收入合计
                $item['zhichubi'] = 0;
                if( $item['ziyingshoufee'] != 0 ){
                    $item['zhichubi'] = round( $item['ziyingzhifee']/$item['ziyingshoufee'] , 2);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_piao_tong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_gong_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.gonger')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai', 'cai'))
            ->order('cai.id DESC')
            ->group('cai.gonger')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger']])->sum('money');
                $item['caijiemoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger']])->sum('caijiemoney');
                $item['shou_money'] =  PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1,'list.gonger'=>$item['gonger']])
                    ->sum('list.money');
                $item['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'duier'=>$item['gonger']])->sum('money');

                $item['yingnofu_anhe'] = round($item['caimoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anjie'] = round($item['caijiemoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anpiao'] = round($item['shou_money'] - $item['zhixiangfee'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_gong_xiang_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.gonger','=',$param['gonger']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.enginid,cai.gonger,e.name,e.business,e.chanid')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai', 'cai'))
            ->order('cai.id DESC')
            ->group('cai.enginid')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('money');
                $item['caijiemoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('caijiemoney');
                $item['shou_money'] =  PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1,'list.gonger'=>$item['gonger'],'shou.enginid'=>$item['enginid']])
                    ->sum('list.money');
                $item['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'duier'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('money');

                $item['yingnofu_anhe'] = round($item['caimoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anjie'] = round($item['caijiemoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anpiao'] = round($item['shou_money'] - $item['zhixiangfee'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_xiang_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.enginid,e.name,e.business,e.chanid,e.state,e.status')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->group('cai.enginid')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);

                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$item['enginid']])->sum('money');
                $item['caijiemoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$item['enginid']])->sum('caijiemoney');
                $item['shou_money'] =  PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1,'shou.enginid'=>$item['enginid']])
                    ->sum('list.money');
                $item['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$item['enginid']])->sum('money');

                $item['yingnofu_anhe'] = round($item['caimoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anjie'] = round($item['caijiemoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anpiao'] = round($item['shou_money'] - $item['zhixiangfee'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_xiang_gong_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.enginid,cai.gonger,e.name')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('cai.id DESC')
            ->group('cai.gonger')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('money');
                $item['caijiemoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'gonger'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('caijiemoney');
                $item['shou_money'] =  PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1,'list.gonger'=>$item['gonger'],'shou.enginid'=>$item['enginid']])
                    ->sum('list.money');
                $item['zhixiangfee'] = ZhiXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'duier'=>$item['gonger'],'enginid'=>$item['enginid']])->sum('money');

                $item['yingnofu_anhe'] = round($item['caimoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anjie'] = round($item['caijiemoney'] - $item['zhixiangfee'], 2);
                $item['yingnofu_anpiao'] = round($item['shou_money'] - $item['zhixiangfee'], 2);


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_com_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }else{
            $param['chanid'] = 0;
        }
        if(!isset($param['year']) || empty($param['year'])){
            $param['year'] = date("Y");
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $list = [
            ['topname' => '项目收入','name' => '投标费','feilei' => (new UserService())->get_optionfeiid_bynum('xmsr_tbf')],
            ['topname' => '项目收入','name' => '工程回款','feilei' => (new UserService())->get_optionfeiid_bynum('xmsr_gchk')],
            ['topname' => '项目收入','name' => '公司垫资','feilei' => (new UserService())->get_optionfeiid_bynum('xmsr_gsdz')],
            ['topname' => '项目收入','name' => '联营方备用金','feilei' => (new UserService())->get_optionfeiid_bynum('xmsr_lyfbyj')],
            ['topname' => '其他收入','name' => '其他收入','feilei' => (new UserService())->get_optionfeiid_bynum('qtsr_qtsr')],
        ];

        foreach ($list as &$item){
            $item['chanid'] = $param['chanid'];
            $item['year'] = $param['year'];
            $item['dt_year'] = $param['year'].'-01-01'.' - '.$param['year'].'-12-31';
            $item['yearfee'] = 0;

            for ($i=1; $i<=12; $i++){
                $yearmonth = $param['year'].'-'.$i.'-01';
                $dt_start = date('Y-m-01', strtotime($yearmonth));
                $dt_end = date('Y-m-d', strtotime($dt_start."+1 month -1 day"));
                $item['dt_'.$i] = $dt_start.' - '.$dt_end;

                $item['month'.$i] = AccountListModel::alias('list')
                    ->field('list.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid
            ,kuan.modtype,kuan.enginid,kuan.zhai,kuan.feilei,kuan.feitype,kuan.duier,kuan.incdaotype
            ,acc.name as acc_name,acc.accname as acc_accname,acc.acctype as acctype,acc.accnum as acc_accnum')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->leftJoin('account acc','acc.id=list.accid')
                    ->where($where)
                    ->where(['kuan.feilei'=>$item['feilei']])
                    ->where('list.dt','between',[$dt_start, $dt_end])
                   ->sum('list.money');

                $item['yearfee'] += $item['month'.$i];
            }
        }




        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_com_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }else{
            $param['chanid'] = 0;
        }
        if(!isset($param['year']) || empty($param['year'])){
            $param['year'] = date("Y");
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $list = [
            ['topname' => '项目支出','name' => '投标费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_tbf')],
            ['topname' => '项目支出','name' => '材料费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_clf')],
            ['topname' => '项目支出','name' => '人工费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_rgf')],
            ['topname' => '项目支出','name' => '机械设备费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_jxsbf')],
            ['topname' => '项目支出','name' => '专业分包费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_zyfbf')],
            ['topname' => '项目支出','name' => '其他费','feilei' => (new UserService())->get_optionfeiid_bynum('xmzc_qtf')],
            ['topname' => '其他支出','name' => '其他支出','feilei' => (new UserService())->get_optionfeiid_bynum('qtzc_qtzc')],
        ];

        foreach ($list as &$item){
            $item['chanid'] = $param['chanid'];
            $item['year'] = $param['year'];
            $item['dt_year'] = $param['year'].'-01-01'.' - '.$param['year'].'-12-31';
            $item['yearfee'] = 0;

            for ($i=1; $i<=12; $i++){
                $yearmonth = $param['year'].'-'.$i.'-01';
                $dt_start = date('Y-m-01', strtotime($yearmonth));
                $dt_end = date('Y-m-d', strtotime($dt_start."+1 month -1 day"));
                $item['dt_'.$i] = $dt_start.' - '.$dt_end;

                $item['month'.$i] = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->leftJoin('account acc','acc.id=list.accid')
                    ->where($where)
                    ->where(['kuan.feilei'=>$item['feilei']])
                    ->where('list.dt','between',[$dt_start, $dt_end])
                   ->sum('list.money');

                $item['yearfee'] += $item['month'.$i];
            }
        }




        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shu_com_year(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }else{
            $param['chanid'] = 0;
        }
        if(isset($param['acc_name']) && !empty($param['acc_name'])){
            $where[] = ['acc.name','like', '%'.$param['acc_name'].'%'];
        }else{
            $param['acc_name'] = '';
        }
        if(isset($param['accid']) && !empty($param['accid'])){
            $where[] = ['list.accid','like', '%'.$param['accid'].'%'];
        }
        if(!isset($param['year']) || empty($param['year'])){
            $param['year'] = date("Y");
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['accid'] = AccountModel::field('id,name,accnum')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();
        foreach ($data['accid'] as $k=>$v){
            $data['accid'][$k]['name'] = $v['name'].$v['accnum'];
        }

        $data['is_accid'] = isset($param['accid']) ? $param['accid'] : '';

        $list = [
            ['month'=>1, 'monthname'=>'一月'],
            ['month'=>2, 'monthname'=>'二月'],
            ['month'=>3, 'monthname'=>'三月'],
            ['month'=>4, 'monthname'=>'四月'],
            ['month'=>5, 'monthname'=>'五月'],
            ['month'=>6, 'monthname'=>'六月'],
            ['month'=>7, 'monthname'=>'七月'],
            ['month'=>8, 'monthname'=>'八月'],
            ['month'=>9, 'monthname'=>'九月'],
            ['month'=>10, 'monthname'=>'十月'],
            ['month'=>11, 'monthname'=>'十一月'],
            ['month'=>12, 'monthname'=>'十二月'],
        ];

        foreach ($list as &$item){
            $item['chanid'] = $param['chanid'];
            $item['acc_name'] = $param['acc_name'];
            $item['year'] = $param['year'];
            $item['dt_year'] = $param['year'].'-01-01'.' - '.$param['year'].'-12-31';
            $item['yearfee'] = 0;



            $yearmonth = $param['year'].'-'.$item['month'].'-01';
            $dt_start = date('Y-m-01', strtotime($yearmonth));
            $dt_end = date('Y-m-d', strtotime($dt_start."+1 month -1 day"));
            $item['dt_month'] = $dt_start.' - '.$dt_end;

            $item['shou_tou'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>1])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['shou_xiang'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>2])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['shou_qi'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>4])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['shou_all'] = round($item['shou_tou']+$item['shou_xiang']+$item['shou_qi'], 2);

            $item['zhi_tou'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>21])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_xiang'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>22])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_enbao'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>23])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_jintui'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>24])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_lianjie'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>25])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_dayfu'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>28])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_daybao'] = AccountListModel::alias('list')->leftJoin('account acc','acc.id=list.accid')
                ->where($where)->where(['list.modtype'=>27])->where('list.dt','between',[$dt_start, $dt_end])->sum('list.money');
            $item['zhi_all'] = round($item['zhi_tou']+$item['zhi_xiang']+$item['zhi_enbao']+$item['zhi_jintui']
                +$item['zhi_lianjie']+$item['zhi_dayfu']+$item['zhi_daybao'], 2);

            $item['shouzhicha'] = round($item['shou_all']-$item['zhi_all'], 2);
        }


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }















}
